// 文章接口
var express = require('express');
var router = express.Router();
const sql = require('../conf/sql.js')
const pool = require('../conf/db.js')
// 获取医院列表（默认id排序）
router.get('/id', function (req, res, next) {
  pool.query(sql.hospital_list, (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});
// 获取医院列表by grade

router.get('/', function (req, res, next) {
  let grade = req.query.grade
  pool.query(sql.hospital_list_grade, [grade], (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});
// 获取医院列表by distance
router.get('/distance', function (req, res, next) {
  pool.query(sql.hospital_list_distance, (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});
// 获取医院列表by score
router.get('/score', function (req, res, next) {
  pool.query(sql.hospital_list_score, (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});

// 医院详情
router.get('/detail', function (req, res, next) {
  let id = req.query.id
  pool.query(sql.hospital_detail, [id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});

// 进入医院挂号界面
router.get('/registration', function (req, res, next) {
  let id = req.query.id
  pool.query(sql.hospital_registration, [id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    } else {
      res.json({
        code: 200,
        msg: 'get hospitallist success',
        data: result
      })
    }

  })

});
module.exports = router;
